package liangtWorkSpace.PBE;

import org.apache.commons.codec.binary.Base64;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
 
public class PBEUtil {
	public static final String salt = "OhHnvpT3I1s=";
	public static final String password = "OhHnvpT3I1s=";
	public static final String ALGORITHM = "PBEWITHMD5andDES";

	public static final int ITERATION_COUNT = 100;

	public static byte[] initSalt() throws Exception {
		//实例化安全随机数
		SecureRandom random = new SecureRandom();
		return random.generateSeed(8);
	}

	/***
	 * 转换密钥
	 * @param password 密码
	 * @return 密钥
	 * @throws Exception
	 */
	private static Key toKey(String password) throws Exception {
		//密钥材料
		PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
		//实例化
		SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
		//生成密钥
		return factory.generateSecret(keySpec);
	}

	/***
	 * 加密
	 * @param data 待加密数据
	 * @param password 密钥
	 * @param salt
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(byte[] data,  String salt,String password) throws Exception {
		byte[] bytes = encrypt(data,password,Base64.decodeBase64(salt));
		return Base64.encodeBase64String(bytes);
	}
	public static byte[] encrypt(byte[] data, String password, byte[] salt) throws Exception {
		//转换密钥
		Key key = toKey(password);
		//实例化PBE参数材料
		PBEParameterSpec spec = new PBEParameterSpec(salt, ITERATION_COUNT);
		//实例化
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		//初始化
		cipher.init(Cipher.ENCRYPT_MODE, key, spec);
		return cipher.doFinal(data);
	}

	public static String encrypt(String data) throws Exception {
		return Base64.encodeBase64String(encrypt(data.getBytes()));
	}

	public static byte[] encrypt(byte[] data) throws Exception {
		//转换密钥
		Key key = toKey(password);
		//实例化PBE参数材料
		PBEParameterSpec spec = new PBEParameterSpec(Base64.decodeBase64(salt), ITERATION_COUNT);
		//实例化
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		//初始化
		cipher.init(Cipher.ENCRYPT_MODE, key, spec);
		return cipher.doFinal(data);
	}



	/***
	 * 解密
	 * @param data 待解密数据
	 * @param password 密钥
	 * @param salt
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String data , String password, String salt) throws Exception {
		return new String(decrypt(Base64.decodeBase64(data),password,Base64.decodeBase64(salt)));
	}
	public static byte[] decrypt(byte[] data, String password, byte[] salt) throws Exception {
		//转换密钥
		Key key = toKey(password);
		//实例化PBE参数材料
		PBEParameterSpec spec = new PBEParameterSpec(salt, ITERATION_COUNT);
		//实例化
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		//初始化  
		cipher.init(Cipher.DECRYPT_MODE, key, spec);
		//执行操作  
		return cipher.doFinal(data);
	}
	public static String decrypt(String data) throws Exception {
		return new String(decrypt(Base64.decodeBase64(data)));
	}
	public static byte[] decrypt(byte[] data) throws Exception {
		//转换密钥
		Key key = toKey(password);
		//实例化PBE参数材料
		PBEParameterSpec spec = new PBEParameterSpec(Base64.decodeBase64(salt), ITERATION_COUNT);
		//实例化
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		//初始化
		cipher.init(Cipher.DECRYPT_MODE, key, spec);
		//执行操作
		return cipher.doFinal(data);
	}


	public static String showByteArray(byte[] data) {
		if (null == data) {
			return null;
		}
		StringBuilder sb = new StringBuilder();
		for (byte b : data) {
			sb.append(b).append(",");
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("");
		return sb.toString();
	}


	public static void main(String[] args) throws Exception {
		String data = "\"测试\":\"123\"";
		String enStr = encrypt(data);
//		String enStr = Base64.encodeBase64String(encryptData);
		System.out.println("加密后数据：byte[]:" +enStr );
		byte[] deByte = Base64.decodeBase64(enStr);
//		String message = "GwOvlNfwHY25SNEaaZbwqyxcDXD/af0L8nEi1ajmpNV982WPuJ2PHC95R+FD0HS7WYT5UrTh7dLAtLxrO7ZNDs1FPcPqiUtmwyVCOu6gKh+Yqx7WTBEJQd8FtM6qAPAnorUlCQ2SysqRJ9dU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password, salt);
//		System.out.println("解密后数据: byte[]:" + Base64.encodeBase64String(decryptData));
//		System.out.println("解密后数据: string:" + new String(decryptData));
		System.out.println("解密后数据: string:" + dStr);
	}
}
